﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmSignIn : Form
    {
        public FrmSignIn()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 存储窗体坐标
        /// </summary>
        public Point p = new Point();

        //窗体加载
        private void FrmSignIn_Load(object sender, EventArgs e)
        {
            this.Location = p;
        }

        //登录
        private void btnLogin_Click(object sender, EventArgs e)
        {
            FrmLogin login = new FrmLogin();
            login.p = this.Location;
            login.Show();
            this.Close();
        }

        #region 注册
        //注册
        private void btnSignIn_Click(object sender, EventArgs e)
        {
            if (!CheckNull())
                return;
            if (!cbxConsent.Checked)
                return;
            if (!txtPwd.Text.Trim().Equals(txtNewPwd.Text.Trim()))
            {
                MessageBox.Show("两次密码不一致！","提示：");
                txtNewPwd.Focus();
                return;
            }
            Role user = new User(txtNewName.Text.Trim(), txtPwd.Text.Trim());
            RoleManage.dirRole.Add(user.Name,user);
            //注册成功后将数据序列化至本地
            //创建文件流
            FileStream fs = new FileStream("data//role.bin", FileMode.Create);
            //创建序列化文本类
            BinaryFormatter bf = new BinaryFormatter();
            //序列化
            bf.Serialize(fs, RoleManage.dirRole);
            fs.Close();
            MessageBox.Show("注册成功！","提示：");
            FrmLogin login = new FrmLogin();
            login.p = this.Location;
            login.Show();
            this.Close();
        }
        #endregion

        #region textbox提示文本
        //textbox文本值更改时发生
        private void TextChangeds(object sender, EventArgs e)
        {
            if (sender.Equals(txtNewName))
            {
                label1.Visible = txtNewName.Text.Length < 1;
            }
            else if (sender.Equals(txtPwd))
            {
                label2.Visible = txtPwd.Text.Length < 1;
            }
            else if (sender.Equals(txtNewPwd))
            {
                label3.Visible = txtNewPwd.Text.Length < 1;
            }
            else if (sender.Equals(txtCode))
            {
                label4.Visible = txtCode.Text.Length < 1;
            }
        }

        //单击label控件发生
        private void label2_Click(object sender, EventArgs e)
        {
            if (sender.Equals(label1))
            {
               txtNewName.Focus();
            }
            else if (sender.Equals(label2))
            {
                txtPwd.Focus();
            }
            else if (sender.Equals(label3))
            {
                txtNewPwd.Focus();
            }
            else if (sender.Equals(label4))
            {
                txtCode.Focus();
            }
        }
        #endregion

        #region 为空验证
        /// <summary>
        /// 为空验证
        /// </summary>
        /// <returns></returns>
        public bool CheckNull()
        {
            if (txtNewName.Text.Trim().Length < 1)
            {
                label1.ForeColor = Color.Red;
                label1.Focus();
                return false;
            }
            else if (txtPwd.Text.Trim().Length < 1)
            {
                label2.ForeColor = Color.Red;
                label2.Focus();                
                return false;
            }
            else if (txtNewPwd.Text.Trim().Length < 1)
            {
                label3.ForeColor = Color.Red;
                label3.Focus();
                return false;
            }
            return true;
        }
        #endregion
    }
}
